package panel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import file.DB;
import file.IOStreamStudent;
import model.Student;

public class DeletePanel extends JFrame implements ActionListener{
	public static DeletePanel instance=new DeletePanel();
	JLabel name=new JLabel("姓名");
	JTextField tfName=new JTextField(10);
	JButton deleteStu=new JButton("删除");
	JPanel panel=new JPanel();
	JOptionPane jOptionPane=new JOptionPane();
	public DeletePanel(){
		this.setTitle("删除学生信息");
		this.setSize(250, 100);
		panel.add(name);
		panel.add(tfName);
		panel.add(deleteStu);
		this.add(panel);
		this.setLocationRelativeTo(null);//位于屏幕中央
		this.setResizable(false);//设置此窗体是否可由用户调整大小
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//设置默认关闭操作
		deleteStu.addActionListener(this);
	}
	public static void main(String[] args){
		DeletePanel.instance.setVisible(true);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		JButton button=(JButton) e.getSource();
		if(button==DeletePanel.instance.deleteStu){
			int flag=jOptionPane.showConfirmDialog(this, "是否删除?");
			if(flag==JOptionPane.YES_OPTION){
				String name=tfName.getText();
				ArrayList<Student> list=new ArrayList<>();
				list=IOStreamStudent.readFromFile();
				for(int i=0;i<list.size();i++){
					if(list.get(i).getCourse().equals(ScorePage.instance.box.getSelectedItem().toString())&&
		        			list.get(i).getTeacher().equals(ScorePage.instance.box2.getSelectedItem().toString())&&
		        			list.get(i).getStu_class().equals(ScorePage.instance.box3.getSelectedItem().toString())){
						if(list.get(i).getName().equals(name)){
							DB.arrStu=IOStreamStudent.readFromFile();
							DB.arrStu.remove(i);
							IOStreamStudent.writeToFile();
						}
					}
				}
				jOptionPane.showMessageDialog(this, "删除成功！");
				DeletePanel.instance.setVisible(false);
			}else{
				ScorePage.instance.setVisible(true);
			}
		}
	}
}
